package lgkj.meicqt20.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.List;

import lgkj.meicqt20.bean.AdverBean;
import lgkj.meicqt20.greendao.AdverBeanDao;
import lgkj.meicqt20.greendao.DaoMaster;
import lgkj.meicqt20.greendao.DaoSession;

/**
 * Created by Administrator on 2019/1/17 0017.
 */

public class DbController {
    private static final String TAG = "DbController";
    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;//获取Helper对象
    /**
     * 数据库
     */
    private SQLiteDatabase db;
    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    /**
     * 上下文
     */
    private Context context;
    /**
     * dao
     */
    private AdverBeanDao personInforDao;

    private static DbController mDbController;

    /**
     * 获取单例
     */
    public static DbController getInstance(Context context) {
        if (mDbController == null) {
            synchronized (DbController.class) {
                if (mDbController == null) {
                    mDbController = new DbController(context);
                }
            }
        }
        return mDbController;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public DbController(Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context, "person.db", null);
        mDaoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        personInforDao = mDaoSession.getAdverBeanDao();
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "person.db", null);
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     *
     * @return
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "person.db", null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    /**
     * 会自动判定是插入还是替换
     *
     * @param personInfor
     */
    public void insertOrReplace(AdverBean personInfor) {
        personInforDao.insertOrReplace(personInfor);
    }

    /**
     * 插入一条记录，表里面要没有与之相同的记录
     *
     * @param personInfor
     */
    public long insert(AdverBean personInfor) {
        if (searchByWhere(personInfor.getAd_order()) == null)
            return personInforDao.insert(personInfor);
        else
            update(personInfor);
        return -1;
    }

    /**
     * 更新数据
     *
     * @param personInfor
     */
    public void update(AdverBean personInfor) {
        AdverBean mOldPersonInfor = personInforDao.queryBuilder().where(AdverBeanDao.Properties.Ad_order.eq(personInfor.getAd_order())).build().unique();//拿到之前的记录
        if (mOldPersonInfor != null) {
            personInfor.setId(personInfor.getId());
            Log.i(TAG, "update: " + personInfor.toString());
            personInforDao.update(personInfor);
        }
    }

    /**
     * 按条件查询数据
     */
    public AdverBean searchByWhere(String wherecluse) {
        AdverBean personInfors = personInforDao.queryBuilder().where(AdverBeanDao.Properties.Ad_order.eq(wherecluse)).build().unique();
        if (personInfors != null) {
            Log.i(TAG, "searchByWhere: " + personInfors.toString());
            return personInfors;
        } else {
            return null;
        }
    }

    /**
     * 查询所有数据
     */
    public List<AdverBean> searchAll() {
        List<AdverBean> personInfors = personInforDao.queryBuilder().list();
        return personInfors;
    }

    /**
     * 删除数据
     */
    public void delete() {
        personInforDao.queryBuilder().buildDelete().executeDeleteWithoutDetachingEntities();
    }
}
